package com.sugar.proxy.cglib;

import com.sugar.proxy.java.MyService;
import com.sugar.proxy.java.MyServiceImpl;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author felix
 * @date 2025/1/31 18:00
 */
public class MyInterceptor implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("before invoke method......");
        Object result = methodProxy.invokeSuper(o, objects);
        System.out.println("after invoke method......");
        return result;
    }

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(MyServiceImpl.class);
        enhancer.setCallback(new MyInterceptor());
        MyService proxy = (MyService)enhancer.create();
        proxy.doSomething();
    }
}
